package cac.fenjian.resource.DeliveryPlanQuery.rest;

import cac.fenjian.resource.DeliveryPlanQuery.service.DeliveryPlanService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/DeliveryPlanQuery/")
public class DeliveryPlanRest {
    @Autowired
    private DeliveryPlanService deliveryPlanService;

    //修改交接单状态(作废改为已移交)
    @PostMapping("/updatedeliveryStatus")
    public HashMap<String,Object> updatedeliveryStatus(@RequestBody HashMap map) {
        HashMap<String,Object> mapData = new HashMap<>();
        List<HashMap> lists = (List<HashMap>) map.get("datas");
        String wmsDeliveryPlanId = String.valueOf(lists.get(0).get("wmsDeliveryPlanId"));
        Integer data = deliveryPlanService.updatedeliveryStatus(wmsDeliveryPlanId);
        System.out.println("data:"+data);
        if (data==0){
            mapData.put("code","-1");
            mapData.put("msg","修改失败,请选择无库存移交并且作废的数据！");
        } else {
            mapData.put("code","1");
            mapData.put("msg","修改成功,为已移交状态！");
        }
        return mapData;
    }

    //修改质量编号
    @PostMapping("/updateQualityCode")
    public void UpdateQualityCode(@RequestBody HashMap map) {
        List<HashMap> lists = (List<HashMap>) map.get("datas");
        String wmsDeliveryPlanId = String.valueOf(lists.get(0).get("wmsDeliveryPlanId"));
        String itemCode = String.valueOf(map.get("itemCode"));
        deliveryPlanService.UpdateQualityCode(wmsDeliveryPlanId, itemCode);
    }

    //库存管理--交接单查询
    @PostMapping("/ParamsByDeliveryQuery")
    public ResponseMsg<QueryRespBean<HashMap>> ParamsByDeliveryQuery(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        HashMap searchParams = JSON.parseObject(searchParamsParams, HashMap.class);

        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);


        QueryRespBean<HashMap> result = deliveryPlanService.DeliverPlanQuery(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp());
        responseMsg.setResponseBody(result);
        return responseMsg;
    }
    @PostMapping("/DeliveryQuery")
    public PageInfo DeliveryQuery(@RequestBody HashMap map) {
        HashMap queryParam = (HashMap) map.get("queryParam");
        HashMap page = (HashMap) map.get("page");
        return deliveryPlanService.DeliverQuery(queryParam,page);
    }

    /**
     * 2023-08-28 撤销移交
     * **/
    @PostMapping("cancelDelivery")
    public ResponseMsg cancelDelivery(@RequestBody List<HashMap> list){
        return deliveryPlanService.cancelDelivery(list);
    }

    /**
     * 2023-11-22 撤销未移交
     * **/
    @PostMapping("cancelUnDelivery")
    public ResponseMsg cancelUnDelivery(@RequestBody List<HashMap> list){
        return deliveryPlanService.cancelUnDelivery(list);
    }

    /**
     * 重新推送配送数据
     * **/
    @PostMapping("reSend")
    public ResponseMsg reSend(@RequestBody List<HashMap> list){
        return deliveryPlanService.reSend(list);
    }
    /**
     * 重新推送备注
     * **/
    @PostMapping("reSendRemark")
    public ResponseMsg reSendRemark(@RequestBody List<HashMap> list){
        return deliveryPlanService.reSendRemark(list);
    }

    /**
     * 更新备注
     * */
    @PostMapping("updateRemark")
    public ResponseMsg updateRemark(@RequestBody HashMap map){
        return deliveryPlanService.updateRemark(map);
    }

    @PostMapping("updateWmsDelivery")
    public ResponseMsg updateWmsDelivery(@RequestBody HashMap map){
        return deliveryPlanService.updateWmsDelivery(map);
    }
    @PostMapping("updateWmsDeliveryStatus")
    public ResponseMsg updateWmsDeliveryStatus(@RequestBody HashMap map){
        return deliveryPlanService.updateWmsDeliveryStatus(map);
    }
}
